package com.xmut.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xmut.mall.product.annotation.CheckJwt;
import com.xmut.mall.product.entity.ProductAttrValueEntity;
import com.xmut.mall.product.service.ProductAttrValueService;
import com.xmut.mall.product.utils.RespBean;
import com.xmut.mall.product.vo.AddAttrVo;
import com.xmut.mall.product.vo.AttrRespVo;
import com.xmut.mall.product.vo.AttrVo;
import com.xmut.mall.product.vo.UpdateAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.product.entity.AttrEntity;
import com.xmut.mall.product.service.AttrService;
import com.xmut.mall.product.utils.PageUtils;
import com.xmut.mall.product.utils.R;



/**
 * 商品属性
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 00:45:33
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 根据cate分类获取销售属性
     * @param cateId
     * @return
     */
    @CheckJwt
    @GetMapping("/getSaleAttr/{cateId}")
    public RespBean getSaleAttr(@PathVariable Long cateId){
        return attrService.getSaleAttr(cateId);
    }

    /**
     * 批量删除属性
     * @param ids
     * @return
     */
    @CheckJwt
    @DeleteMapping("/deleteAttr")
    public RespBean deleteAttr(@RequestBody List<Long> ids){
        System.out.println("测试参数:"+ids);
        return attrService.deleteAttr(ids);
    }

    /**
     * 编辑attr信息
     * @param vo
     * @return
     */
    @CheckJwt
    @PutMapping("/updateAttr")
    public RespBean updateAttr(@RequestBody UpdateAttrVo vo){
        return attrService.editAttr(vo);
    }

    /**
     * 添加属性
     * @param vo
     * @return
     */
    @CheckJwt
    @PostMapping("/addAttr")
    public RespBean addAttr(@RequestBody AddAttrVo vo){
        return attrService.addAttr(vo);
    }

    /**
     * 获取普通属性分页
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getAttrByPage")
    public RespBean getAttrByPage(@RequestBody Map<String,Object> params){
        return attrService.getAttrByPage(params);
    }

    /**
     * 根据当前的catId获取属于当前CateId且可用于检索的attr属性
     * @param cateId
     * @return
     */
    @GetMapping("/getAbleToSearchAttrByCateId/{cateId}")
    public RespBean getAbleToSearchAttrByCateId(@PathVariable Long cateId){
        List<AttrEntity> arrts = attrService.getAbleToSearchAttrByCateId(cateId);
        return RespBean.success("获取attr成功",arrts);
    }

    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);

        return R.ok();
    }

    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);

        return R.ok().put("data",entities);
    }

    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type){
        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo respVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
